﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Systems.Model
{
    public interface IUser
    {
        string UserId { get; set; }
        string LoginName { get; set; }
        string FullName { get; set; }
        string Password { get; set; }
        string AssetCode { get; set; }
        string AssetName { get; set; }
        string Email { get; set; }
        int IsAdmin { get; set; }
        DateTime Dob { get; set; }
        int Sex { get; set; }
        int Locked { get; set; }
        DateTime FailLoginTime { get; set; }
        int FailLoginNumber { get; set; }

        bool IsSystemAdmin{get;}

        decimal Budget { get; set; }
        string RoleId { get; set; }
        DateTime RegDate { get; set; }
        DateTime LastAccessedDate { get; set; }
    }
    
    public class UserCls : IUser
    {
        string mUserId = "";
        public string UserId
        {
            get
            {
                return mUserId;
            }
            set
            {
                mUserId = value;
            }
        }


        int _Locked = 0;

        public int Locked
        {
            get { return _Locked; }
            set { _Locked = value; }
        }

        string mRoleId = "";
        public string RoleId
        {
            get
            {
                return mRoleId;
            }
            set
            {
                mRoleId = value;
            }
        }


        DateTime _FailLoginTime = new System.DateTime();

        public DateTime FailLoginTime
        {
            get { return _FailLoginTime; }
            set { _FailLoginTime = value; }
        }
        int _FailLoginNumber = 0;

        public int FailLoginNumber
        {
            get { return _FailLoginNumber; }
            set { _FailLoginNumber = value; }
        }

        string mRoleName = "";
        public string RoleName
        {
            get
            {
                return mRoleName;
            }
            set
            {
                mRoleName = value;
            }
        }

        string mLoginName = "";
        public string LoginName
        {
            get
            {
                return mLoginName;
            }
            set
            {
                mLoginName = value;
            }
        }

        string mEmail = "";
        public string Email
        {
            get
            {
                return mEmail;
            }
            set
            {
                mEmail = value;
            }
        }
        
        string mFullName = "";
        public string FullName
        {
            get
            {
                return mFullName;
            }
            set
            {
                mFullName = value;
            }
        }


        string mPhone = "";
        public string Phone
        {
            get
            {
                return mPhone;
            }
            set
            {
                mPhone = value;
            }
        }
        string mPassword = "";
        public string Password
        {
            get
            {
                return mPassword;
            }
            set
            {
                mPassword = value;
            }
        }
        string mAssetCode = "";
        public string AssetCode
        {
            get
            {
                return mAssetCode;
            }
            set
            {
                mAssetCode = value;
            }
        }
        
        string mAssetName = "";
        public string AssetName
        {
            get
            {
                return mAssetName;
            }
            set
            {
                mAssetName = value;
            }
        }

        string mNickName = "";
        public string NickName
        {
            get
            {
                return mNickName;
            }
            set
            {
                mNickName = value;
            }
        }

        DateTime mRegDate = new System.DateTime();
        public DateTime RegDate
        {
            get
            {
                return mRegDate;
            }
            set
            {
                mRegDate = value;
            }
        }


        DateTime mLastAccessedDate = new System.DateTime();
        public DateTime LastAccessedDate
        {
            get
            {
                return mLastAccessedDate;
            }
            set
            {
                mLastAccessedDate = value;
            }
        }

        int mIsAdmin = 0;
        public int IsAdmin
        {
            get
            {
                return mIsAdmin;
            }
            set
            {
                mIsAdmin = value;
            }
        }
        DateTime mDob = new System.DateTime();
        public DateTime Dob
        {
            get
            {
                return mDob;
            }
            set
            {
                mDob = value;
            }
        }
        int mSex = 0;
        public int Sex
        {
            get
            {
                return mSex;
            }
            set
            {
                mSex = value;
            }
        }
       
        public bool IsSystemAdmin
        {
            get
            {
                return LoginName.Trim().ToLower().Equals("admin");
            }
        }

      
        decimal mBudget = 0;
        public decimal Budget
        {
            get
            {
                return mBudget;
            }
            set
            {
                mBudget = value;
            }
        }

        public RoleCls mRoleModel=null;
        public RoleCls RoleModel
        {
            get
            {
                if (mRoleModel == null) mRoleModel = new RoleCls();
                mRoleModel.RoleId = RoleId;
                mRoleModel.RoleName = RoleName;
                
                return mRoleModel;
            }
        }

        
    }

    public class UserParser
    {
        public static UserCls CreateInstance()
        {
            //tao 1 class trang....
            UserCls oUserCls = new UserCls();
            return oUserCls;
        }

        public static UserCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            UserCls oUserCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oUserCls;
        }


        public static UserCls ParseFromDataRow(DataRow dr)
        {
            UserCls OUserCls = new UserCls();
            OUserCls.UserId = (string)dr["UserId"];
            OUserCls.LoginName = (string)dr["LoginName"];
            OUserCls.FullName = (string)dr["FullName"];
            if (dr["Password"] != DBNull.Value)
            {
                OUserCls.Password = (string)dr["Password"];
            }
            OUserCls.AssetCode = (string)dr["AssetCode"];
            try
            {
                OUserCls.AssetName = (string)dr["AssetName"];
            }
            catch { }
            try
            {
                OUserCls.Email = (string)dr["Email"];
            }
            catch { }
            try
            {
                OUserCls.RoleId = (string)dr["RoleId"];
            }
            catch { }
            try
            {
                OUserCls.RegDate = (DateTime)dr["RegDate"];
            }
            catch { }
            try
            {
                OUserCls.LastAccessedDate = (DateTime)dr["LastAccessedDate"];
            }
            catch { }
            try
            {
                OUserCls.IsAdmin = (int)dr["IsAdmin"];
            }
            catch { }

            try
            {
                OUserCls.Locked = (int)dr["Locked"];
            }
            catch { }


            try
            {
                OUserCls.FailLoginNumber = (int)dr["FailLoginNumber"];
            }
            catch { }

            try
            {
                OUserCls.FailLoginTime = (DateTime)dr["FailLoginTime"];
            }
            catch { }

            return OUserCls;
        }

        public static UserCls[] ParseFromDataTable(DataTable dtTable)
        {
            UserCls[] Users = new UserCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                Users[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return Users;
        }
    }
}

